﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Cell : MonoBehaviour
{
    [Header("Param")]
    public int x = 0;
    public int y = 0;
    public int id;
    public int target_y;
    public int camp = 1; //阵营:1为我方 2为敌方
    public int number;
    public float cellSize;
    //public bool isSelect = false;
    //public bool isLastCell = false;
    //public bool isMove = false;

    [Header("Component")]
    public GameObject Parent_element;
    public SpriteRenderer element;

    //public Dictionary<string, object> data;

    void Start()
    {
        
    }

    public void Init(int _id, int _x, int _y)
    {
        id = _id;
        x = _x;
        y = _y;
        cellSize = ShuaiManager.Instance.cellSize;

        this.gameObject.name = "Cell_" + (x + 1) + "_" + (y + 1);
        this.transform.localScale = new Vector3(cellSize, cellSize, 1);
        this.transform.position = CellFunctions.GetPos(x, y);
    }

    public void Refresh()
    {
        RefreshUI();

        RefreshPos();
    }

    public void RefreshUI()
    {
        Parent_element.gameObject.SetActive(id > 0);
        element.sprite = SpriteManager.Instance.cellSpriteList[id - 1];
    }

    public void RefreshPos()
    {
        var pos_x = this.cellSize * this.x - ShuaiManager.Instance.width / 2 + this.cellSize / 2;
        var pos_y = this.cellSize * this.y - ShuaiManager.Instance.height_screen / 2 + this.cellSize / 2 + ShuaiManager.Instance.paddingBottom;
        this.transform.position = new Vector3(pos_x, pos_y, 0);
    }

    public void Set(int _x, int _y)
    {
        ShuaiManager.Instance.cellList[this.x][this.y] = null;

        this.x = _x;
        this.y = _y;

        ShuaiManager.Instance.cellList[this.x][this.y] = this;
    }

    public void Clear()
    {
        ShuaiManager.Instance.cellList[x][y] = null;
        Destroy(this.gameObject);
    }

    public void Dead()
    {
        ShuaiManager.Instance.cellList[x][y] = null;
        Destroy(this.gameObject);

        GameObject go = GameObject.Instantiate(ShuaiManager.Instance.prefab_Effect_list[this.id - 1]);
        go.transform.parent = ShuaiManager.Instance.ParentElement.transform;
        go.transform.position = gameObject.transform.position;

        ShuaiFunctions.DelayFunction(() =>
        {
            Destroy(go.gameObject);
        }, 1f);
    }


}
